package io.renren.common.imp;

import io.renren.common.utils.ImpUtils;
import io.renren.modules.sys.entity.JxStudentEntity;
import io.renren.modules.sys.service.JxStudentService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 导入学生实现了
 */
@Component
public class JxStudentImp extends ImpUtils {

    @Autowired
    JxStudentService jxStudentService;

    /**
     * 实现业务
     */
    @Override
    public Map<String,Object> businessDo(Sheet sheet, int len, Map<String, Object> param) {
        Map<String,Object>  re=new HashMap<String, Object>();  //   返回结果

        Map<String,Object> dataMp=new HashMap<String, Object>(); //  返回数据的结果
        Map<String,Object>  storeMp=new HashMap<String,Object>(); //   返回数据库数据   用于去重
        Long schoolId = Long.valueOf(param.get("schoolId").toString()); //   学校
        Long classId = Long.valueOf(param.get("classId").toString()); //  班级
        String salt = RandomStringUtils.randomAlphanumeric(20);
        String defind_password = ShiroUtils.sha256(JxStudentEntity.DEFIND_PASSWORD, salt);
        List<JxStudentEntity> list1 = new ArrayList<JxStudentEntity>(); //   xml 数据存储
        List<JxStudentEntity> list = new ArrayList<JxStudentEntity>();
        list=jxStudentService.getALLlist();
        for(JxStudentEntity entity:list){   // 需要通过学去重
         storeMp.put(entity.getStudentNum(),entity);
        }
        Date day = new Date();
        String text="";
        int errorSize=0;
        String errorMsg="";
        for (int i = 1; i < len + 1; i++) {
            //读取左上端单元格
            Row row = sheet.getRow(i);
            //行不为空
            if (row != null) {
                //读取cell
                String name = getCellValue(row.getCell(0)); //
                String studentNum = getCellValue(row.getCell(1)); //
                String sex=getCellValue(row.getCell(2)); //
                String phoneNum=getCellValue(row.getCell(3)); //
                JxStudentEntity entity = new JxStudentEntity();
                entity.setName(name);
                entity.setPassword(defind_password);
                entity.setClassId(classId);
                entity.setSchoolId(schoolId);
                entity.setSalt(salt);
                entity.setStudentNum(studentNum);
                entity.setSex(sex.equals("男")?"0":"1");
                entity.setPhoneNum(phoneNum);
                entity.setIcon(JxStudentEntity.DEFIND_ICON);
                entity.setCreateTime(day);
                if(dataMp.containsKey(studentNum)){
                    re.put("errorMsg","导入重复数据 \n");
                    text=text+"第"+(i+1)+"行数据与第"+Integer.parseInt(dataMp.get(studentNum).toString())+"行数据重复 \n";
                    errorSize=+1;
                    continue;
                }
                dataMp.put(studentNum,i+1);
                if(storeMp.containsKey(studentNum)){
                    re.put("errorMsg","导入重复数据 \n");
                    text=text+"第"+(i+1)+"行数据与数据库中的数据重复 \n";
                    errorSize=+1;
                    continue;
                }
               list1.add(entity);
            }

        }
        if(!(errorSize>0)){
            int in= jxStudentService.addBatchStudent(list1);
            re.put("errorMsg","成功导入"+in+"条数据");
            System.out.println("成功导入"+in+"条数据");
        }
            re.put("text",text);
            re.put("errorSize",errorSize);

        return  re;
    }


}
